/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.knezevic.realestate.pages;

import com.knezevic.realestate.Identifiable;
import com.knezevic.realestate.Member;
import com.knezevic.realestate.beans.AdvertBean;
import com.knezevic.realestate.beans.RealEstateBean;
import com.knezevic.realestate.service.Adverts;
import com.knezevic.realestate.service.Members;
import com.knezevic.realestate.service.RealEstates;
import java.io.File;
import java.util.Date;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.services.Context;
import org.apache.tapestry5.upload.services.UploadedFile;

/**
 *
 * @author Nemanja
 */
public class CreateAdvert {
    private Member member;
    @Property
    @Persist("flash")
    private String message;
    @Inject
    private Adverts adverts;
    @Inject
    private RealEstates realEstates;
    @Property
    private AdvertBean advert;
    @Property
    @Persist
    private UploadedFile image;
    @Inject
    private Members members;
    @Inject
    private Context c;
    

    void onActivate(long id){
        member = members.retrieve(id);
        advert = new AdvertBean();
        advert.setRealEstate(new RealEstateBean());
    }

    long onPassivate(){
        return Identifiable.class.cast(member).getID();
    }

    /**
     * @return the member
     */
    public Member getMember() {
        return member;
    }

    Object onSubmit(){

        RealEstateBean r = new RealEstateBean();
        AdvertBean a = new AdvertBean();
        if(image==null){
            r = new RealEstateBean(advert.getRealEstate().getTypeOfRealEstate(), advert.getRealEstate().getLocation(), advert.getRealEstate().getSquareMeters(), advert.getRealEstate().getPrice(), advert.getRealEstate().getDescription());
            realEstates.save(r);
            a = new AdvertBean(member, new Date(), advert.getTypeOfAdvert(),r, "", false);
            adverts.save(a);

        }else{
            r = new RealEstateBean(advert.getRealEstate().getTypeOfRealEstate(), advert.getRealEstate().getLocation(), advert.getRealEstate().getSquareMeters(), advert.getRealEstate().getPrice(), advert.getRealEstate().getDescription());
            realEstates.save(r);
            a = new AdvertBean(member, new Date(), advert.getTypeOfAdvert(),r, image.getFileName(), false);
            adverts.save(a);
            File webapp = c.getRealFile("/");
            String newImage = webapp.getPath()+"\\images\\"+Identifiable.class.cast(a).getID()+".jpg";
            image.write(new File(newImage));
        }
        message = "You have successfully created new advert! You must wait for administartor to approve it!";
        return null;
    }
    /**
     * @param member the member to set
     */
    public void setMember(Member member) {
        this.member = member;
    }
    Object onActionFromLogout(){
        return Login.class;
    }

    public long getID(){
        return Identifiable.class.cast(member).getID();
    }

}
